﻿using System.Collections.Generic;
using studentSubsidy.Dao;
using studentSubsidy.Entity;
using studentSubsidy.Models;

namespace studentSubsidy.Service
{
    public class FinancialService
    {
        private FinancialDao _financialDao = new FinancialDao();
        private ApplyDao _applyDao = new ApplyDao();
        private AdminFinancialDao _adminFinancialDao = new AdminFinancialDao();

        public Result getNeedToPayment(int userId)
        {
            List<Apply> applies = _financialDao.getNeedToPayment(userId);
            List<Project> projectInfo = new List<Project>();
            foreach (var VARIABLE in applies)
            {
                Project project = _applyDao.getProjectById(VARIABLE.project_id);
                if (project.isRefund == 1 && VARIABLE.isSigned != 1)
                {
                    continue;
                }

                project.category_id = VARIABLE.apply_id;
                projectInfo.Add(project);
            }

            return new Result(200, "搜索成功", projectInfo, null);
        }

        public Result getHasPaymentList(int userId)
        {
            List<Apply> applies = _financialDao.getHasPaymentList(userId);
            List<Project> projectInfo = new List<Project>();
            foreach (var VARIABLE in applies)
            {
                Project project = _applyDao.getProjectById(VARIABLE.project_id);
                projectInfo.Add(project);
            }

            return new Result(200, "搜索成功", projectInfo, null);
        }

        public Result getNeedDueList(int userId)
        {
            List<Apply> applies = _financialDao.getNeedDueList(userId);

            List<Project> projectInfo = new List<Project>();
            foreach (var VARIABLE in applies)
            {
                Refund refund = _adminFinancialDao.getRefundByApplyId(VARIABLE.apply_id);
                if (refund.state == 1)
                {
                    continue;
                }

                Project project = _applyDao.getProjectById(VARIABLE.project_id);
                project.category_id = VARIABLE.apply_id;
                projectInfo.Add(project);
            }

            return new Result(200, "搜索成功", projectInfo, null);
        }

        public Result PaymentDue(int refundLogId)
        {
            Refund_Log refundLog = _financialDao.getRefundLogById(refundLogId);
            refundLog.hasRefund = 1;
            _financialDao.updateRefundLogState(refundLog);
            Refund refund = _financialDao.getRefundById(refundLog.refund_id);
            refund.cur_price = refund.cur_price - refundLog.refund_price;
            if (refund.cur_price < 0)
            {
                refund.cur_price = 0;
            }
            _adminFinancialDao.updateRefundState(refund);

            List<Refund_Log> refundLogs = _adminFinancialDao.getRefundLogByRefundId(refundLog.refund_id);
            bool isRefundSuccess = true;
            foreach (var VARIABLE in refundLogs)
            {
                if (VARIABLE.hasRefund == 0)
                {
                    isRefundSuccess = false;
                }
            }

            if (isRefundSuccess == true)
            {
                refund.state = 1;
                _adminFinancialDao.updateRefundState(refund);
            }

            return new Result(200, "还款成功", null, null);
        }

        public Result getFinishProject(int userId)
        {
            List<Apply> applies = _financialDao.getNeedDueList(userId);

            List<Project> projectInfo = new List<Project>();
            foreach (var VARIABLE in applies)
            {
                Refund refund = _adminFinancialDao.getRefundByApplyId(VARIABLE.apply_id);
                if (refund.state != 1)
                {
                    continue;
                }

                Project project = _applyDao.getProjectById(VARIABLE.project_id);
                project.category_id = VARIABLE.apply_id;
                projectInfo.Add(project);
            }

            return new Result(200, "搜索成功", projectInfo, null);
        }
        
    }
}